/*
 * author : Dinh Ngoc Tuan Vu (A0092633)
 */
package sg.edu.nus.iss.team4ft.ui;

import java.awt.*;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;

import javax.swing.*;

import sg.edu.nus.iss.team4ft.service.CustomerAccount;
import sg.edu.nus.iss.team4ft.util.StringUtil;

public class CustomerAccountDetails extends Panel{
	
	/**
	 * author : Dinh Ngoc Tuan Vu
	 */
	private static final long serialVersionUID = 1L;
	
	private ArrayList<CustomerAccount> originalCustomers = new ArrayList<CustomerAccount>();
	private ArrayList<CustomerAccount> listCustomers = new ArrayList<CustomerAccount>();
	private List listOfCustomers;
	
	private JLabel search = new JLabel("Search");
	private Choice filterBy = new Choice();
	private TextField  value = new TextField (20);
	
	public CustomerAccountDetails(ArrayList<CustomerAccount> listCustomers){
	//public CustomerAccountDetails(ArrayList<CustomerAccount> listCustomers, String filterBy, String value){
		this.setPreferredSize(new java.awt.Dimension(1000,560));
		this.originalCustomers =  new ArrayList<CustomerAccount>(listCustomers);
		this.listCustomers = new ArrayList<CustomerAccount>(listCustomers);
		this.setLayout(new BorderLayout());
        //filterRecord(filterBy, value);
		loadCustomers();
		
		//this.add ("North", new JLabel ("Customer Details"));
        //this.add ("Center", listOfCustomers);
        //this.add ("East", createFilterPanel());
		
       
		/*value.addTextListener(new TextListener() {
				
		@Override
		public void textValueChanged(TextEvent arg0) {
			try {
				filterRecord(filterBy.getItem(filterBy.getSelectedIndex()), value.getText());
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}				
			}
		});*/
        
        value.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
            	try {
					filterRecord(filterBy.getItem(filterBy.getSelectedIndex()), value.getText());
				} catch (CloneNotSupportedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }			
        });
	}
	public void loadCustomers(ArrayList<CustomerAccount> customers){
		listCustomers = customers;	
		loadCustomers();
	}
	private void loadCustomers(){	
		listOfCustomers = null;

		listOfCustomers = new List(listCustomers.size() + 1);
        listOfCustomers.setMultipleMode (false);
        String heading = "Customer ID" + StringUtil.getEmptySpace("Customer ID")+ "        " + "Account ID" + StringUtil.getEmptySpace("Account ID")+
        		"Name" + StringUtil.getEmptySpace("Name") + "Contact Number" + StringUtil.getEmptySpace("Contact Number") + "1# Mobile Number" + StringUtil.getEmptySpace("1# Mobile Number")
        		+ "1# Line Number" + StringUtil.getEmptySpace("1# Line Number") + "Status";
        listOfCustomers.add(heading);
		
		for(CustomerAccount ca : listCustomers){
			listOfCustomers.add(ca.toString());
		}    

		this.removeAll();
		
		this.add ("North", new JLabel ("Customer Details"));
        this.add ("Center", listOfCustomers);
        this.add ("East", createFilterPanel());
		
		//listOfCustomers.invalidate();
		listOfCustomers.validate();
		//listOfCustomers.repaint();
		this.invalidate();
		this.validate();
		this.repaint();
		value.setFocusable(true);
	}
	
	public JPanel createFilterPanel(){
		
		JPanel p = new JPanel (new GridLayout (0,1));
		
		p.add(search);
		
		filterBy.removeAll();
		filterBy.add("Customer ID");
		filterBy.add("Account ID");
		filterBy.add("Name");
		filterBy.add("Contact Number");
		
		ItemListener lFilter = new ItemListener(){
			public void itemStateChanged(ItemEvent e){
				filterBy.getSelectedIndex();
			}
		};
		filterBy.addItemListener(lFilter);	
        p.add (filterBy);
        p.add(new JPanel());
        
        /*value.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					filterRecord(filterBy.getItem(filterBy.getSelectedIndex()), value.getText());
				} catch (CloneNotSupportedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
        	
        });*/
        p.add (value);
        p.add(new JPanel());
        
        JPanel bp = new JPanel ();
        bp.setLayout (new BorderLayout());
        bp.add ("North", p);
        return bp;
	}
	
	public CustomerAccount getCustomer(){
		int idx = listOfCustomers.getSelectedIndex();
		if (idx < 1){
			return null;			
		}else{
			return listCustomers.get(idx-1);
		}
	}
	
	/*private void filterRecord(FilterBy filterBy, String value) throws CloneNotSupportedException{
		if(value.equals(""))
			this.listCustomers = new ArrayList<CustomerAccount>(this.originalCustomers);
		else{
			switch(filterBy){
			case CustomerID:
				for (CustomerAccount c : this.originalCustomers) {
					if(c.getCustomer().getCustomerId().contains(value))
						this.listCustomers.add(c);
				}break;
			case AccountID:
				for (CustomerAccount c : this.originalCustomers) {
					if(c.getAccount().getAccountNumber().contains(value))
						this.listCustomers.add(c);
				}break;
			case Name:			
				for (CustomerAccount c : this.originalCustomers) {
					if(c.getCustomer().getFirstName().contains(value) || c.getCustomer().getLastName().contains(value) )
						this.listCustomers.add(c);
				}break;
			}
		}
		loadCustomers();

	}*/
	
	private void filterRecord(String filterBy, String value) throws CloneNotSupportedException{
		//this.listCustomers = new ArrayList<CustomerAccount>();
		ArrayList<CustomerAccount> temp = new ArrayList<CustomerAccount>();
		if(value.equals(""))
			//this.listCustomers = new ArrayList<CustomerAccount>(this.originalCustomers);
			temp = new ArrayList<CustomerAccount>(this.originalCustomers);
		else{
			if (filterBy == "Customer ID"){
				for (CustomerAccount c : this.originalCustomers) {
					if(c.getCustomer().getCustomerId().toLowerCase().contains(value.toLowerCase()))
						//this.listCustomers.add(c);
						temp.add(c);
				}
			}else if(filterBy == "Account ID"){
				for (CustomerAccount c : this.originalCustomers) {
					if(c.getAccount().getAccountNumber().toLowerCase().contains(value.toLowerCase())){
						//this.listCustomers.add(c);
						temp.add(c);
					}
				}
			}else if(filterBy == "Name"){
				for (CustomerAccount c : this.originalCustomers) {
					if(c.getCustomer().getFirstName().toLowerCase().contains(value.toLowerCase()) || c.getCustomer().getLastName().toLowerCase().contains(value.toLowerCase()) ){
						//this.listCustomers.add(c);
						temp.add(c);
					}
				}
			}else if(filterBy == "Contact Number"){
				for (CustomerAccount c : this.originalCustomers) {
					if(c.getCustomer().getTelephoneNumber().toString().contains(value)){
						//this.listCustomers.add(c);
						temp.add(c);
					}
				}
			}
		}
		//loadCustomers(listCustomers);
		loadCustomers(temp);
	}
}


enum FilterBy{CustomerID, AccountID, Name, ContactNumber, MobileNumber, LineNumber};
